App = {
    web3Provider: null,
    contracts: {},
    account: '0x0',
    flag: 1,

    init: function () {
        return App.initWeb3();
    },

    initWeb3: function () {
        if (typeof window.ethereum !== 'undefined') {
            // If a web3 instance is already provided by Meta Mask.
            App.web3Provider = window.ethereum;
            web3 = new Web3(window.ethereum);
        } else {
            // Specify default instance if no web3 instance provided
            App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
            web3 = new Web3(App.web3Provider);
        };
        return App.initContract();
    },

    initContract: function () {
        $.getJSON("Info.json", function (infomation) {
            // Instantiate a new truffle contract from the artifact
            App.contracts.Info = TruffleContract(infomation);
            // Connect provider to interact with contract
            App.contracts.Info.setProvider(App.web3Provider);
            return App.render();
        });
    },

    render: function () {

        // Load account data
        web3.eth.getCoinbase(function (err, account) {
            if (err === null) {
                App.account = account;
                console.log(account);
            }
        });
    },

    userCount: function () {
        App.contracts.Info.deployed().then(function (instance) {
            InfoInstance = instance;
            console.log(InfoInstance.usercount());
            return InfoInstance.usercount();
        }).then(function (usercount) {
            console.log(usercount);
            return;
        })
        return;
    },

    AddUser: async function () {
        var flag = 1;
        var username = $("#useName").val();
        var password1 = $("#password1").val();
        var password2 = $("#password2").val();
        var email = $("#email").val();
        var phone = $("#phone").val();
        if (password1 != password2) {
            console.log("The password is different!");
            flag = 0;
        }
        if (flag == 1) {
            App.contracts.Info.deployed().then(async function (instance) {
                InfoInstance = instance;

                result = await InfoInstance.addUser(email, password1, username, phone, App.account, {from:App.account,gas:'1000000'});
                res = result.logs[0].args._result;
                console.log(res);
                if(res){
                    console.log("AddUser successfully!");
                    alert("AddUser successfully!");
                }else {
                    console.log("AddUser fail!");
                    alert("AddUser fail!");
                }
                /*window.location.href = "index.html";*/
                return;
            }).catch(function (error) {
                console.warn(error);
            });
        }
        return;
    },

    informationCheck: async function () {
        var userName = $("#useName").val();
        var password = $("#password").val();
        var result;
        var userInstance;

        //载入用户信息
        App.contracts.Info.deployed().then(async function (instance) {
            userInstance = instance;
            return await userInstance.usercount();
        }).then(async function (usercount) {
            //遍历区块链中的用户名查看是否有先相同
            console.log(usercount);
            for (var i = 1; i <= usercount; i++) {

                await userInstance.users(i).then(async function (user) {
                    var user_name = user[2];
                    var password_ = user[1];
                    //对密码加密操作
                    /*password = CryptoJS.SHA256(password);*/
                    console.log(user[2]);
                    console.log(user[1]);
                    result = await userInstance.login(userName, password, { from: App.account, gas: '1000000' });
                    /*password = CryptoJS.enc.Base64.stringify(password);*/
                    console.log(result);
                    console.log(typeof(result));
                    if (result == true) {
                        alert("login successfully!");
                        App.flag = await 0;
                        id = await userName;
                        window.location.href = "html/Community.html?value="+id;
                        //跳转界面

                    }
                });
            }
            if (App.flag == 1) {
                console.log("The user is not exist!");
                return;
            }
        }).catch(function (err) {
            console.warn(err);
        });
    },
};

$(function () {
    $(window).load(function () {
        App.init();
    });
});
